/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.dntuan.tool;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import com.lowagie.text.Document;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfImportedPage;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfWriter;

/**
 *
 * @author dang nhu tuan
 */
public class SplitPDF {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        try {
            SplitPDF.splitPDF(new FileInputStream("E:\\ebook\\Java\\fundamental/Java Reflection in Action.pdf"),
                    new FileOutputStream("D:\\chapter8.pdf"), 202, 229);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * @author viralpatel.net
     *
     * @param inputStream Input PDF file
     * @param outputStream Output PDF file
     * @param fromPage start page from input PDF file
     * @param toPage end page from input PDF file
     */
    public static void splitPDF(InputStream inputStream, OutputStream outputStream, int fromPage, int toPage) {
        Document document = new Document();
        try {
            PdfReader inputPDF = new PdfReader(inputStream);

            int totalPages = inputPDF.getNumberOfPages();

            //make fromPage equals to toPage if it is greater
            if (fromPage > toPage) {
                fromPage = toPage;
            }
            if (toPage > totalPages) {
                toPage = totalPages;
            }

            // Create a writer for the outputstream
            PdfWriter writer = PdfWriter.getInstance(document, outputStream);

            document.open();
            PdfContentByte cb = writer.getDirectContent(); // Holds the PDF data
            PdfImportedPage page;

            while (fromPage <= toPage) {
                document.newPage();
                page = writer.getImportedPage(inputPDF, fromPage);
                cb.addTemplate(page, 0, 0);
                fromPage++;
            }
            outputStream.flush();
            document.close();
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (document.isOpen()) {
                document.close();
            }
            try {
                if (outputStream != null) {
                    outputStream.close();
                }
            } catch (IOException ioe) {
                ioe.printStackTrace();
            }
        }
    }
}
